package com.unitd.modules.configcenter.entity;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.unitd.comm.entity.DataEntity;
import com.unitd.frame.comm.utils.StringUtils;

import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc 操作日志信息表表对应实体对象
 * @filename OperateLogs.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date  2018-01-30
 */
@Table(name = "operate_logs")
public class OperateLogs extends DataEntity {

	/** 请求发起人ID */
	@Column(name = "uid")
	private String uid;
	/** 请求发起人名称 */
	@Column(name = "uname")
	private String uname;
	/** 具体请求操作 */
	@Column(name = "act_name")
	private String actName;
	/** 请求操作的数据 */
	@Column(name = "biz_data")
	private String bizData;
	/** 请求前 */
	@Column(name = "before_data")
	private String beforeData;
	/** 请求后 */
	@Column(name = "after_data")
	private String afterData;
	/** 请求发起IP */
	@Column(name = "ip_addr")
	private String ipAddr;
	/** 请求发起时间 */
	@Column(name = "act_time")
	private Date actTime;

	/* ************************** 与数据库字段不直接关联而是对应外键对象或数据处理的字段 ************************** */
	@Transient
	private Map<String, Object> bizDataMap = new HashMap<>();

	public OperateLogs() {
		super();
	}

	public OperateLogs(String id){
		super(id);
	}

	public OperateLogs(String id, String uid, String uname, String actName, String bizData, String beforeData, String afterData, String ipAddr, Date actTime){
		this.id = id;
		this.uid = uid;
		this.uname = uname;
		this.actName = actName;
		this.bizData = bizData;
		this.beforeData = beforeData;
		this.afterData = afterData;
		this.ipAddr = ipAddr;
		this.actTime = actTime;
	}

	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getActName() {
		return actName;
	}
	public void setActName(String actName) {
		this.actName = actName;
	}
	public String getBizData() {
		if (StringUtils.isBlank(bizData) && bizDataMap.size() > 0) {
			bizData = JSON.toJSONString(bizDataMap);
		}
		return bizData;
	}
	public void setBizData(String bizData) {
		this.bizData = bizData;
	}
	public String getBeforeData() {
		return beforeData;
	}
	public void setBeforeData(String beforeData) {
		this.beforeData = beforeData;
	}
	public String getAfterData() {
		return afterData;
	}
	public void setAfterData(String afterData) {
		this.afterData = afterData;
	}
	public String getIpAddr() {
		return ipAddr;
	}
	public void setIpAddr(String ipAddr) {
		this.ipAddr = ipAddr;
	}
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getActTime() {
		return actTime;
	}
	public void setActTime(Date actTime) {
		this.actTime = actTime;
	}
	public Map<String, Object> getBizDataMap() {
		return bizDataMap;
	}
	public void setBizDataMap(Map<String, Object> bizDataMap) {
		this.bizDataMap = bizDataMap;
	}

	public OperateLogs addBizData(String key, Object value) {
		this.bizDataMap.put(key, value);
		return this;
	}
}